



//
//  StudyHardViewController.m
//  ZQLearn
//
//  Created by macfeel on 2019/4/29.
//  Copyright © 2019 macfeel. All rights reserved.
//

#import "StudyHardViewController.h"
#import "TripleImgCell.h"
#import "NewsModel.h"
#import "ZNewsDetailViewController.h"
@interface StudyHardViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic,assign)NSInteger currentPage;

@end

#define cellIdentifier @"tri"

@implementation StudyHardViewController
-(UITableView *)tableView{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0 , 1.5, ScreenWidth , ScreenHeight - kTabHeight-ScreenHeightFlex-52) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 168.f;
        _tableView.backgroundColor = kBackgroundColor;
        [_tableView registerNib:[UINib nibWithNibName:@"TripleImgCell" bundle:nil] forCellReuseIdentifier:cellIdentifier];
        
                WeakStament(ws);
                _tableView.mj_header = [CustomRefreshHeader headerWithRefreshingBlock:^{
                    [ws loadData];
                }];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.tableFooterView = [UIView new];
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setItemWithTitle:@"学习强国" textColor:kNavigationTitleColor fontSize:18 itemType:center];
    self.currentPage = 1;
    //    self.view.backgroundColor = kMagentaColor;
    self.dataArray = [NSMutableArray array];
    
    
    
    [self.view addSubview:self.tableView];
    
    [self loadData];
    // Do any additional setup after loading the view.
}
- (void)endRefresh{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}
- (void)loadData{
    
    if (self.currentPage==1) {
        [self.dataArray removeAllObjects];
    }
    NSDictionary *dic = @{@"columnInfo.id":[UserModel shareInstance].newsId,@"pageSize":@(20),@"pageIndex":@(self.currentPage)};
    YYLog(@"%@",[UserModel shareInstance].newsId);
    [ZNetworkService postRequestWithBaseUrl:kNewsListUrl setParameters:dic success:^(id result) {
        
        YYLog(@"%@",result);
        WeakStament(ws);
        if ([result[@"errorCode"]integerValue]==0) {
            NSArray *arr = result[@"result"];
            for (NSDictionary *dic in arr) {
                NewsModel *model = [NewsModel mj_objectWithKeyValues:dic];
                
                [ws.dataArray addObject:model];
            }
            
            [ws.tableView reloadData];
            [ws endRefresh];
        }else{
            
            [AFToast showText:result[@"errorMsg"]];
            [ws endRefresh];
        }
        
    } fail:^(NSError *error) {
        YYLog(@"%@",error);
                [self endRefresh];
    } delegater:nil];
}



#pragma mark - datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ///扯淡，数组没有初始化，一直不走这个代理
    NewsModel *model = self.dataArray[indexPath.row];
    TripleImgCell *cell;
    cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier forIndexPath:indexPath];
    cell.titleLab.text = model.title;
    cell.timeLab.text = model.updateTime;
    if (model.photo.length>25) {
        ///
        NSArray *array = [model.photo componentsSeparatedByString:@","];
        [cell setCellWithPicArray:array];
//        YYLog(@"%@",array[0]);
        
    }else{
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",kPicServerUrl,model.photo]];
        [cell.leftImg sd_setImageWithURL:url placeholderImage:nil];
    }
    
    cell.countLab.text = [NSString stringWithFormat:@"%ld次",(long)model.browseNumber];
    return cell;
}

#pragma delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    NewsModel *model = self.dataArray[indexPath.row];
    NewsModel *model = self.dataArray[indexPath.row];
    ZNewsDetailViewController *news = [ZNewsDetailViewController new];
    //        news.htmlString = model.content;
    news.model = model;
    [self.navigationController pushViewController:news animated:YES];
}


#pragma mark ZJScrollPageViewChildVcDelegate
- (void)zj_viewWillAppearForIndex:(NSInteger)index{
    
    YYLog(@"%ld",index);
    [UserModel shareInstance].tabIndex = index;
    //    self.currentIndex = index;
    
}
- (void)zj_viewDidAppearForIndex:(NSInteger)index{
    
}
- (void)zj_viewWillDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidDisappearForIndex:(NSInteger)index{
    
}
- (void)zj_viewDidLoadForIndex:(NSInteger)index{
    //这里具体处理请求逻辑不同的页面根据索引进行请求
    
    //    [self loadData];
    
}

@end
